﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using PokemonBattle.PokemonData.Custom;

namespace PokemonBattle.PokemonData
{
    [Serializable()]
    public class TeamData : DataStream, ICloneable, IEquatable<TeamData>
    {
        public PokemonCustomInfo[] Pokemons { get; private set; }
        public CustomDataInfo CustomInfo { get; set; }

        public TeamData()
        {
            Pokemons = new PokemonCustomInfo[6];
            for (int i = 0; i < Pokemons.Length; i++)
            {
                Pokemons[i] = new PokemonCustomInfo();
            }
            CustomInfo = new CustomDataInfo();
        }

        #region ICloneable

        public TeamData Clone()
        {
            TeamData data = new TeamData();
            data.CustomInfo.DataHash = CustomInfo.DataHash;
            data.CustomInfo.DataName = CustomInfo.DataName;
            for (int i = 0; i < Pokemons.Length; i++)
            {
                data.Pokemons[i] = (PokemonCustomInfo)Pokemons[i].Clone();
            }
            return data;
        }

        object ICloneable.Clone()
        {
            return this.Clone();
        }

        #endregion

        #region IEquatable

        public bool Equals(TeamData other)
        {
            if (other == null) return false;
            for (int i = 0; i < 6; i++)
            {
                if (!Pokemons[i].Equals(other.Pokemons[i])) return false;
            }
            if (CustomInfo.DataName != other.CustomInfo.DataName) return false;
            if (CustomInfo.DataHash != other.CustomInfo.DataHash) return false;
            return true;
        }

        #endregion

        #region override

        public override void Load(Stream input)
        {
            BinaryReader reader = new BinaryReader(input);
            this.CustomInfo.DataName = reader.ReadString();
            this.CustomInfo.DataHash = reader.ReadString();
            for (int i = 0; i < this.Pokemons.Length; i++)
            {
                this.Pokemons[i] = FromStream<PokemonCustomInfo>(input);
            }
        }

        public override void Save(Stream output)
        {
            BinaryWriter writer = new BinaryWriter(output);
            writer.Write(CustomInfo.DataName);
            writer.Write(CustomInfo.DataHash);
            foreach (PokemonCustomInfo pm in Pokemons)
            {
                pm.Save(output);
            }
        }

        #endregion

        public byte[] ToBytes()
        {
            byte[] bytes;
            using (MemoryStream stream = new MemoryStream())
            {
                Save(stream);
                bytes = new byte[(int)stream.Position];
                Array.Copy(stream.GetBuffer(), bytes, bytes.Length);
            }
            return bytes;
        }

        public static TeamData FromBytes(byte[] bytes)
        {
            TeamData data = null;
            using (MemoryStream stream = new MemoryStream())
            {
                stream.Write(bytes, 0, bytes.Length);
                stream.Seek(0, SeekOrigin.Begin);
                data = FromStream<TeamData>(stream);
            }
            return data;
        }

        public void CheckData()
        {
            foreach (PokemonCustomInfo pm in Pokemons)
            {
                if (pm.Identity == 0) continue;
                if (!BattleData.CheckPokemon(pm))
                {
                    pm.SelectedTrait = 1;
                    pm.SelectedMoves[0] = BattleData.GetPokemon(pm.Identity).Moves[0].MoveName;
                    pm.SelectedMoves[1] = "";
                    pm.SelectedMoves[2] = "";
                    pm.SelectedMoves[3] = "";
                }
                pm.CheckData();
            }
        }

        public static TeamData FromStream(Stream input)
        {
            return FromStream<TeamData>(input);
        }

        /// <summary>
        /// Form From 不分
        /// </summary>
        public static TeamData FormStream(Stream input)
        {
            return FromStream<TeamData>(input);
        }
    }
}
